package persistent.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import model.TStudent;
import model.VStudent;
import persistent.dao.StudentDAO;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.LogUtil;

/**
 * 学生管理业务类接口的实现
 * @author djf
 * @since 2018/10/2
 */
public class StudentImpl implements StudentDAO {
	private HibBaseDAO dao=null;
	private static final Log log = LogFactory.getLog(ParentInfoImpl.class);
	
	public StudentImpl() {
		dao=new HibBaseDAO();
	}

	/**
	 * 添加一个学生
	 * @param studet 学生对象
	 * @return 执行成功返回主键，失败返回0
	 */
	@Override
	public int addStudent(TStudent student) {
		return dao.insert(student);
	}
	
	/**
	 * 根据身份证号码验证学生是否已经存在
	 * @param IDNumber 身份证号码
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public boolean isexist(String IDNumber) {
		String hql = "from TStudent where IDNumber = ?";
		Object[] para = { IDNumber };
		List list = dao.select(hql, para);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
		
	}

	/**
	 * 修改一个学生对象
	 * @param student 学生对象
	 * @return 执行成功返回true，失败返false
	 */
	@Override
	public boolean updateStudent(TStudent student) {
		TStudent student2=(TStudent)dao.findById(TStudent.class, student.getId());
		student2.setStuid(student.getStuid());
		student2.setPwd(student.getPwd());
		student2.setRealName(student.getRealName());
		student2.setSex(student.getSex());
		student2.setBirthday(student.getBirthday());
		student2.setClassid(student.getClassid());
		student2.setSchoolid(student.getSchoolid());
		return dao.update(student2);
	}

	/**
	 * 根据学号得到一个学生对象
	 * @param stuid 学号
	 * @return 成功返回一个学生对象，失败返回null
	 */
	@Override
	public TStudent getTStudent(int id) {
		return (TStudent)dao.findById(TStudent.class, id);
	}
	
	/**
	 * 根据username查询用户信息
	 * @param username
	 * @return
	 */
	@Override
	public VStudent getVStudent(int id) {
		return (VStudent)dao.findById(VStudent.class, id);
	}
	
	/**
	 * 删除一个学生信息
	 * @param stuid 学生id
	 * @return 执行成功返回true，失败返false
	 * @since 2018-11-24 11:08
	 */
	@Override
	public boolean deleteStudent(int stuid) {
		TStudent student = 
				(TStudent) dao.findById(TStudent.class, stuid);		
		return dao.delete(student);
	}

	/**
	 * 根据查询条件得到一个学生对象列表
	 * @param strwhere 查询条件
	 * @return 成功返回TStudent对象列表，失败返回null
	 */
	@Override
	public List<VStudent> getStudentList(String strwhere) {
		String hql="from VStudent "+strwhere;
		return dao.select(hql);
	}

	/**
	 * 根据查询条件得到一个分页的学生对象列表
	 * @param strwhere 查询条件
	 * @param currentPage 当前页
	 * @param pageSize 每页数
	 * @return 成功返回TStudent对象列表，失败返回null
	 */
	@Override
	public List<VStudent> getStudentListBywhere(String strwhere,
			int currentPage, int pageSize) {
		String hql="from VStudent "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}
	
	/**
	 * 批量导入学生信息 
	 * @param list 学生信息list
	 * @return 成功返回true，失败返回false
	 * @since 2019-01-10 14:24徐震霖修改
	 */
	@Override
	public boolean batchImport(List<TStudent> stuList) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();

			List<String> stuids = new ArrayList<String>();
			int stuidSize = 0;

			for (int i = 0; i < stuList.size(); i++) {
				String classid = stuList.get(i).getClassid();
				if (i == 0) {
					Expression exp = new Expression();
					exp.andLike("stuid", classid, String.class);
					String hql = "from VStudent " + exp.toString();
					List<TStudent> students = dao.select(hql);
					if (students != null) {
						stuidSize = students.size();
					} else {
						if (tx != null) tx.rollback();
						session.close(); // 关闭连接
						return false;
					}
				}
				if (stuidSize == 0) {
					if (i + 1 <= 9) {
						stuids.add(classid + "00" + (i + 1));
					} else if (i + 1 > 9 && i + 1 < 99) {
						stuids.add(classid + "0" + (i + 1));
					} else {
						stuids.add(classid + (i + 1));
					}
				} else if (stuidSize > 0 && stuidSize < 9) {
					if (stuidSize + i + 1 <= 9) {
						stuids.add(classid + "00" + (stuidSize + i + 1));
					} else if (stuidSize + i + 1 > 9 && stuidSize + i + 1 < 99) {
						stuids.add(classid + "0" + (stuidSize + i + 1));
					} else {
						stuids.add(classid + (stuidSize + i + 1));
					}
				} else {
					if (stuidSize + i + 1 <= 9) {
						stuids.add(classid + "00" + (stuidSize + i + 1));
					} else if (stuidSize + i + 1 > 9 && stuidSize + i + 1 < 99) {
						stuids.add(classid + "0" + (stuidSize + i + 1));
					} else {
						stuids.add(classid + (stuidSize + i + 1));
					}
				}
			}

			for (int i = 0; i < stuList.size(); i++) {

				//System.out.println(stuids.get(i));
				TStudent tParentInfo = stuList.get(i);
				tParentInfo.setStuid(stuids.get(i));
				tParentInfo.setPwd(Constant.STARTPASSWORD);
				session.save(tParentInfo);
			}
			tx.commit();
			return true;
		} catch (Exception e) {
			if (tx != null) tx.rollback();
			session.close(); // 关闭连接
			log.error("StudentImpl类batchImport(List<TStudent> stuList)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("StudentImpl类batchImport(List<TStudent> stuList)方法调用错误：");
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 根据班级id生成学生账号 
	 * @param classid 班级id
	 * @return 成功返回学生账号，失败返回null
	 * @since 2019-01-10 14:24徐震霖添加
	 */
	@Override
	public String generateStuUserName(String classid) {
		Expression exp = new Expression();
		exp.andLike("stuid", classid, String.class);
		String hql = "from VStudent " + exp.toString();
		List<TStudent> students = dao.select(hql);
		if (students != null) {
			if (students.size() == 0) {
				return classid + "001";
			} else if (students.size() > 0 && students.size() < 9) {
				return classid + "00" + (students.size() + 1);
			} else {
				return classid + "0" + (students.size() + 1);
			}
		}
		return null;
	}

}
